using System;
using System.Collections.Generic;
using axCommerce.Data.Interface;

namespace axCommerce.Data
{
	public partial class Comment : IComparable<Comment>, ISimpleData
	{
		public List<Comment> ChildComments { get; set; }

		public RatingCollection RatingInfo { get; private set; }

		public Comment()
		{
			ParentCommentId = 0;
		}

		private void FillChildComments()
		{
			if (CommentId.HasValue)
				ChildComments = GetChildComments(CommentId.Value);
		}

		private void FillRatingInfo()
		{
			if (CommentId.HasValue)
				RatingInfo = new RatingCollection(CommentId.Value, false);
		}

		private List<Comment> GetChildComments(int iParentCommentId)
		{
			return Search(new Comment
			              	{
			              		ParentCommentId = iParentCommentId
			              	});
		}

		public static List<Comment> GetAllComments(int iProductId)
		{
			return Search(new Comment
			              	{
			              		ProductId = iProductId
			              	});
		}

		public void Save()
		{
			CreatedDate = DateTime.Now;
			LastModifiedDate = DateTime.Now;
			IsDeleted = false;

			Insert();
		}

		public void UpdateInstance()
		{
			LastModifiedDate = DateTime.Now;

			Update();
		}

		public void MarkAsDeleted()
		{
			LastModifiedDate = DateTime.Now;
			IsDeleted = true;

			Update();
		}

		public int CompareTo(Comment other)
		{
			return LastModifiedDate != null && other.LastModifiedDate != null
			       	? LastModifiedDate.Value.CompareTo(other.LastModifiedDate.Value)
			       	: 0;
		}
	}
}

